package com.funny.learning.english.activity;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.funny.learning.english.GameBaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.model.HistoryEntity;
import com.funny.learning.english.model.LangNgheVaVietEntity;
import com.funny.learning.english.util.Utils;

public class LangNgheVaVietActivity extends GameBaseActivity implements OnClickListener{
	
	private LinearLayout llListWord;
	private LayoutInflater inflater;
	
//	private ArrayList<DataDapAnEntity> arrDataWordDapan1 = new ArrayList<DataDapAnEntity>();
//	private ArrayList<DataDapAnEntity> arrDataWordDapan2 = new ArrayList<DataDapAnEntity>();
//	private ArrayList<DataDapAnEntity> arrDataWordDapan3 = new ArrayList<DataDapAnEntity>();
	
	private ArrayList<String> arrDataWord1 = new ArrayList<String>();
	private ArrayList<String> arrDataWord2 = new ArrayList<String>();
	private ArrayList<String> arrDataWord3 = new ArrayList<String>();
	
	private ArrayList<String> arrDataWordDapan = new ArrayList<String>();
	private ArrayList<String> arrDataWord = new ArrayList<String>();
	
//	private ArrayList<NgheTiengVaVietEntity> arrDataNgheViet = new ArrayList<NgheTiengVaVietEntity>();
	
	private int countTrue = 0;
	
	private int countScreen = 0;
	
	private Animation animation6;
	
	private TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8,tv9,tv10;
	private Animation animation1;
	private Animation animation2;
	private Animation animation3;
	private Animation animation4;
	private Animation animation5;
	private Animation animation9;
	private Animation animation7;
	private Animation animation8;
	private Animation animation10;
	private ImageView ivDes;
	private int srcSound;
	private ImageView ivSound;
	private ImageView ivHelp;
	
	private String slugGame = "";
	private String slugTopic = "";
	private String slugSubTopic = "";
	
	private int countSumScreen = 0;
	
	private String TAG = "LangNgheVaVietActivity";
	
	private boolean isPause = false;
	private ImageView ivPlay;
	private ImageView ivRefresh;
	private LinearLayout llPause;
	private TextView tvTitle;
	private ImageView ivPause;
	private LinearLayout llBack;
	private ImageView ivBack;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lang_nghe_viet_layout);
		
		ivPause = (ImageView)findViewById(R.id.ivPause);
		ivPlay = (ImageView)findViewById(R.id.ivPlay);
		ivRefresh = (ImageView)findViewById(R.id.ivRefresh);
		llBack = (LinearLayout)findViewById(R.id.llBack);
		ivBack = (ImageView)findViewById(R.id.ivBack);
		llPause = (LinearLayout)findViewById(R.id.llPause);
		tvTitle = (TextView)findViewById(R.id.tvTitle);
		
		ivBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View paramView) {
				onBackPressed();
			}
		});
		
		
		ivRefresh.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
				countScreen = 0;
				initData(countScreen);
			}
		});
		
		llBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				onBackPressed();
			}
		});
//		
		ivPause.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				isPause = !isPause;
				if(isPause){
					ivPause.setImageResource(R.drawable.ic_play);
					llPause.setVisibility(View.VISIBLE);
				}else{
					ivPause.setImageResource(R.drawable.ic_pause);
					llPause.setVisibility(View.GONE);
				}
			}
		});
		
		ivPlay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
			}
		});
		
		
		inflater = LayoutInflater.from(activity);
		initData();
		
		
	}
	private void refreshDataResult(ArrayList<LangNgheVaVietEntity> arrDataWordDapan){
		 if(llListWord != null){
			 llListWord.removeAllViews();
		 }
		 int lengthArr = arrDataWordDapan.size();
		 if(lengthArr > 0){
			 for (int i = 0; i < arrDataWordDapan.size(); i++) {
				 
				 View view = inflater.inflate(R.layout.langngh_viet_item, null);
				
				 TextView tvWord = (TextView)view.findViewById(R.id.tvWord);
				 LinearLayout llIndex = (LinearLayout)view.findViewById(R.id.llIndex);
				 
				 LangNgheVaVietEntity langNgheVaVietEntity = arrDataWordDapan.get(i);
				 if(langNgheVaVietEntity != null){
//					if(!Character.isWhitespace(arrDataWordDapan.get(i).getWord().charAt(0))){
						tvWord.setText(arrDataWordDapan.get(i).getWord());
						
						 if(arrDataWordDapan.get(i).isVisible()){
							 llIndex.setBackgroundResource(android.R.color.transparent);
							 tvWord.setVisibility(View.VISIBLE);
						 }else{
							 llIndex.setBackgroundResource(R.drawable.bg_text_invi1);
							 tvWord.setVisibility(View.INVISIBLE);
						 }
//			         }else{
//			        	 tvWord.setText(" ");
//			        	 llIndex.setBackgroundResource(android.R.color.transparent);
//			        	 tvWord.setVisibility(View.VISIBLE);
//			         }
					 
					 llListWord.addView(view);
				 }
			 }
		 }
		 llListWord.invalidate();
	 }
	
	private void loadDataGrid(){
		
		tv1.setText(arrDataWord.get(0).toUpperCase());
		tv2.setText(arrDataWord.get(1).toUpperCase());
		tv3.setText(arrDataWord.get(2).toUpperCase());
		tv4.setText(arrDataWord.get(3).toUpperCase());
		tv5.setText(arrDataWord.get(4).toUpperCase());
		tv6.setText(arrDataWord.get(5).toUpperCase());
		tv7.setText(arrDataWord.get(6).toUpperCase());
		tv8.setText(arrDataWord.get(7).toUpperCase());
		tv9.setText(arrDataWord.get(8).toUpperCase());
		if(arrDataWord.get(9) != null){
			tv10.setText(arrDataWord.get(9).toUpperCase());
		}else{
			tv10.setText(Utils.randomString());
		}
	}

	
	private void initData(){
		
		Log.d(TAG, "initData");
		
		ivDes = (ImageView)LangNgheVaVietActivity.this.findViewById(R.id.ivDes);
		
		ivSound = (ImageView)LangNgheVaVietActivity.this.findViewById(R.id.ivSound);
		ivHelp = (ImageView)LangNgheVaVietActivity.this.findViewById(R.id.ivHelp);
		
		tv1 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv1);
		tv2 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv2);
		tv3 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv3);
		tv4 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv4);
		tv5 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv5);
		tv6 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv6);
		tv7 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv7);
		tv8 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv8);
		tv9 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv9);
		tv10 = (TextView)LangNgheVaVietActivity.this.findViewById(R.id.tv10);
		
		ivSound.setOnClickListener(this);
		ivHelp.setOnClickListener(this);
		
		
		llListWord = (LinearLayout)LangNgheVaVietActivity.this.findViewById(R.id.llListWord);
		
		animation1 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation2 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation3 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation4 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation5 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation6 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation9 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation7 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation8 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		animation10 = AnimationUtils.loadAnimation(activity, R.anim.g_mw_wrong);
		
		setAnimation(animation1, tv1);
		setAnimation(animation2, tv2);
		setAnimation(animation3, tv3);
		setAnimation(animation4, tv4);
		setAnimation(animation5, tv5);
		setAnimation(animation6, tv6);
		setAnimation(animation7, tv7);
		setAnimation(animation8, tv8);
		setAnimation(animation9, tv9);
		setAnimation(animation10, tv10);
		
		initData(countScreen);
	}
	
	private void setAnimation(final Animation animation, final TextView view) {
	view.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(final View arg01) {
			for (int i = 0; i < arrLangNgheVaViet.size(); i++) {
				LangNgheVaVietEntity dataDapAnEntity = arrLangNgheVaViet.get(i);

				String textClick = view.getText().toString();

				if (dataDapAnEntity.getWord().toLowerCase().contains(textClick.toLowerCase())) {
					if (arrLangNgheVaViet.get(countTrue).getWord().equals(textClick)) {
						view.setBackgroundResource(R.drawable.chon_tu_dung);
						arrLangNgheVaViet.get(countTrue).setVisible(true);
						refreshDataResult(arrLangNgheVaViet);
						countTrue = countTrue + 1;
						if (countTrue >= arrLangNgheVaViet.size()) {
							countTrue = 0;
							if(countCheckScreen < countScreen){
								countCheckScreen = countCheckScreen + 1;
								countAnwserCorrect = countAnwserCorrect + 1;
							}
							countScreen = countScreen + 1;
							Log.d(TAG, "countScreen::" + countScreen);
							if (countScreen >= countSumScreen) {
								finishGame();
								HistoryEntity historyEntity = new HistoryEntity(1, thumbTopic, slugGameName, countAnwserCorrect, mGameEntities.size(), (int) System.currentTimeMillis(), slugTopicName, slugSubTopicName);
								mHistoryDAO.saveHistory(historyEntity);
							} else {
								initData(countScreen);
							}
						}
					} else {
						if(countCheckScreen < countScreen){
							countCheckScreen = countCheckScreen + 1;
						}
						view.setBackgroundResource(R.drawable.chon_tu_sai);
					}

					break;
				} else {
					view.setBackgroundResource(R.drawable.chon_tu_sai);
				}
			}

			view.startAnimation(animation);

			animation.setAnimationListener(new AnimationListener() {
				@Override
				public void onAnimationStart(Animation arg0) {
				}

				@Override
				public void onAnimationRepeat(Animation arg0) {
				}

				@Override
				public void onAnimationEnd(Animation arg0) {
					view.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
				}
			});

		}
	});
}
	
	
	@Override
	protected void initSucess() {
		super.initSucess();
		
		countSumScreen = mGameEntities.size();
		
//		initData();
//		initData(countScreen);
	}
	
	
	
	private ArrayList<LangNgheVaVietEntity> arrLangNgheVaViet = new ArrayList<LangNgheVaVietEntity>();
	
	private void initData(int count){
//		Log.d(TAG, "getSearchEN::" + mGameEntities.get(count).getLname());
		
		if(mGameEntities.get(count).getLaudio() != null){
			playMusic(getPathSound(mGameEntities.get(count)));
		}
		
		Bitmap bm1 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(mGameEntities.get(count))),getResources().getDimensionPixelSize(R.dimen.game_img_big), getResources().getDimensionPixelSize(R.dimen.game_img_big), true);
		
		ivDes.setImageBitmap(bm1);
		
		String str = mGameEntities.get(count).getLname();
		if(str == null){
			countScreen = countScreen + 1;
			if (countScreen >= countSumScreen) {
				finishGame();
			}else{
				initData(countScreen);
			}
		}else{
			// Load data hien thi O ket qua va du lieu chon
			loadWordData(mGameEntities.get(count).getLname());

			// Hien thi du lieu du lieu Ket qua
			refreshDataResult(arrLangNgheVaViet);
		}
	}
	
	private void loadWordData(String strResult){
		arrDataWord = new ArrayList<String>();
		arrLangNgheVaViet = new ArrayList<LangNgheVaVietEntity>();
		char[] cResult = strResult.toCharArray();
		int sizeWord = cResult.length;
		
		Log.d(TAG, "sizeWord::" + sizeWord);
		
		for (int i = 0; i < sizeWord; i++) {
			String strWord = String.valueOf(cResult[i]).toUpperCase();
			Log.d(TAG, "sizeWord::" + strWord);
			arrLangNgheVaViet.add(new LangNgheVaVietEntity(strWord, false));
			if(!arrDataWord.contains(strWord)){
				arrDataWord.add(strWord);
			}
		}
		switch (arrDataWord.size()) {
		case 2:
			addMoreWordData(2);
			break;
		case 3:
			addMoreWordData(3);
			break;
		case 4:
			addMoreWordData(4);
			break;
		case 5:
			addMoreWordData(5);
			break;
		case 6:
			addMoreWordData(6);
			break;
		case 7:
			addMoreWordData(7);
			break;
		case 8:
			addMoreWordData(8);
			break;
		case 9:
			addMoreWordData(9);
			break;

		default:
			break;
		}
	}
	
	private void addMoreWordData(int size){
		if(size == 9){
			String str = Utils.randomString();
			if(!arrDataWord.contains(str)){
				arrDataWord.add(str);
			}else{
				addMoreWordData(arrDataWord.size());
			}
		}else{
			for (int i = 0; i < (10 - size); i++) {
				String str = Utils.randomString();
				if(!arrDataWord.contains(str)){
					Log.d(TAG, "addMoreWordData::" + str);
					arrDataWord.add(str);
				}
			}
			if(arrDataWord.size() < 10){
				addMoreWordData(arrDataWord.size());
			}
		}
		// Hien thi du lieu hien co de chon
		loadDataGrid();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ivPause:
			isPause = !isPause;
			if(isPause){
				ivPause.setImageResource(R.drawable.ic_play);
				llPause.setVisibility(View.VISIBLE);
			}else{
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
			}
			break;
		case R.id.ivPlay:
			ivPause.setImageResource(R.drawable.ic_pause);
			llPause.setVisibility(View.GONE);
			break;
		case R.id.ivRefresh:
			
			break;
		case R.id.llBack:
			onBackPressed();
			break;
		case R.id.ivHelp:
			
			arrLangNgheVaViet.get(countTrue).setVisible(true);
			refreshDataResult(arrLangNgheVaViet);
			countTrue = countTrue + 1;
			if (countTrue >= arrLangNgheVaViet.size()) {
				countTrue = 0;
				if(countCheckScreen < countScreen){
					countCheckScreen = countCheckScreen + 1;
				}
				countScreen = countScreen + 1;
				Log.d(TAG, "countScreen::" + countScreen);
				if (countScreen >= countSumScreen) {
					finishGame();
					HistoryEntity historyEntity = new HistoryEntity(1, thumbTopic, slugGameName, countAnwserCorrect, mGameEntities.size(), (int) System.currentTimeMillis(), slugTopicName, slugSubTopicName);
					mHistoryDAO.saveHistory(historyEntity);
				} else {
					new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							initData(countScreen);
							
						}
					}, 500);
					
				}
			}
		
			break;

		default:
			break;
		}
	}
	
}
